package com.distribute.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.bag.SynchronizedSortedBag;
import org.apache.http.protocol.HTTP;
import org.apache.poi.openxml4j.util.Nullable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.common.util.HttpClientUtil;
import com.distribute.common.util.JsonUtils;
import com.distribute.portal.pojo.BootstrapResult;
import com.distribute.pojo.Order;
import com.distribute.portal.service.OrderService;

/*
 * @version 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {

	@Value("${ORDER_BASE_URL}")
	private String ORDER_BASE_URL;
	@Value("${ORDER_CREATE_URL}")
	private String ORDER_CREATE_URL;
	@Value("${ORDER_GET_BYUSER_URL}")
	private String ORDER_GET_BYUSER_URL;
	@Value("${ORDER_GET_BYORDER_URL}")
	private String ORDER_GET_BYORDER_URL;

	@Override
	public String createOrder(Order order) {
		// 调用创建订单服务之前补全用户信息。
		// 从cookie中后取TT_TOKEN的内容，根据token调用sso系统的服务根据token换取用户信息。
		System.out.println(order+"666666666666666666666666666666666666");
		// 调用distribute-order的服务提交订单。
		String json = HttpClientUtil.doPostJson(ORDER_BASE_URL + ORDER_CREATE_URL, JsonUtils.objectToJson(order));
		System.out.println(json);
		// 把json转换成distributeResult
		DistributeResult distributeResult = DistributeResult.format(json);
		if (distributeResult.getStatus() == 200) {
			Object orderId = distributeResult.getData();
			return orderId.toString();
		} 
		return "";
	}

	@Override
	public Order getOrderById(Long orderId) {
		String json = HttpClientUtil.doGet(ORDER_BASE_URL + ORDER_GET_BYORDER_URL + "/" + orderId);
		DistributeResult result = DistributeResult.formatToPojo(json, Order.class);
		if (result.getStatus() == 200) {
			Order order = (Order) result.getData();
			return order;
		}
		return null;
	}

	@Override
	public BootstrapResult getOrderByUserId(Long userId,Integer page) {
		BootstrapResult bResult = new BootstrapResult();
		// 查询参数
		Map<String, String> param = new HashMap<>();
		param.put("page", page+"");
		String json = HttpClientUtil.doGet(ORDER_BASE_URL + ORDER_GET_BYUSER_URL + "/" + userId,param);
		/*System.out.println( "********************************************" +json);*/
		DistributeResult result = DistributeResult.format(json);
		if (result.getStatus() == 200) {
			System.out.println(
					result.getData() + "********************************************" + result.getData().getClass());
			LinkedHashMap map = (LinkedHashMap) result.getData();
		/*	List<com.distribute.pojo.Order> list5 =  (List<com.distribute.pojo.Order>) (map.get("orderList"));
			System.out.println(
				 "********************************************" + list5.getClass());*/
			/*for (Iterator iterator = list5.iterator(); iterator.hasNext();) {
				Order order2 = (Order) iterator.next();
				
				System.out.println("00000:"+order2.getCreateTime());
				System.out.println("00001:"+order2.getCreateTime().toLocaleString());
				System.out.println("00001:"+order2.getCreateTime().toGMTString());
			}*/
			bResult.setOrderList((List<com.distribute.pojo.Order>) map.get("orderList"));
			/*for (Order order : bResult.getOrderList()) {

				System.out.println("00000:"+order.getCreateTime());
				System.out.println("00001:"+order.getCreateTime().toLocaleString());
				System.out.println("00001:"+order.getCreateTime().toGMTString());
			}*/
			bResult.setTotalPages( (int) map.get("totalPages"));
			return bResult;
		}
		return null;
	}

}